package com.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

/**
 * @author 军大
 */
@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");
        String auto = request.getParameter("auto");
        //校验成功 跳转到网站首页
        if("a".equals(userName)&&"a".equals(password)){
            //如果选择了自动登录，就会保存一个有时间的usession
            if("auto".equals(auto)){
                HttpSession session = request.getSession(true);
                session.setAttribute("loginSucessSession","okok");
                session.setMaxInactiveInterval(6000);
            }else{
                //如果没有选自动登录，就会保存一个session，会话结束就死亡
                HttpSession session = request.getSession(true);
                session.setAttribute("loginSucessSession","okok");
                session.setMaxInactiveInterval(6000);
            }
            request.getRequestDispatcher("/index.jsp").forward(request, response);
        }else{
            request.getRequestDispatcher("/login.jsp").forward(request, response);
        }
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}